package com.petshop.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.common.constant.BalzLogger;
import com.common.constant.ProjectConstant;
import com.common.constant.URLConstant;
import com.common.controller.BalzMD5Generate;
import com.octo.captcha.service.image.DefaultManageableImageCaptchaService;
import com.petshop.model.Shop;
import com.petshop.model.User;
import com.petshop.model.UserProfile;
import com.petshop.model.UserRole;
import com.petshop.service.RegisterService;
import com.petshop.service.RegisterServiceImpl;
import com.petshop.service.ShopService;
import com.petshop.service.ShopServiceImpl;
import com.petshop.service.UserProfileService;
import com.petshop.service.UserProfileServiceImpl;

public class RegisterAction extends HttpServlet {
	
	BalzLogger logger = new BalzLogger();
	
	public static final String FORM_CAPTCHA_KEY="j_captcha";
	private String captchaParameter = FORM_CAPTCHA_KEY;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doPost(request, response); // if request in method get >> do Post
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		    
		logger.debug("Check >> "+request.getParameter("check"));
		logger.debug("UserName >> "+request.getParameter("username"));
		logger.debug("Password >> "+request.getParameter("password"));
		logger.debug("Telephone >> "+request.getParameter("tel"));
		logger.debug("Address >> "+request.getParameter("address"));
		
		boolean usernameExist = true;
		boolean emailExist = true;
		
		if (request.getParameter("username") != null) {
			usernameExist = usernameCheck(request, response);
		}
		
		if (request.getParameter("email") != null) {
			emailExist = emailCheck(request, response);
		}
		
		logger.debug("usernameExist >> "+usernameExist);
		
		//Register
		String check = request.getParameter("check");
		logger.debug("Check 2 >> "+check);
		if(check.equals("N")){
			RegisterService registerService = new RegisterServiceImpl();
			UserProfile userProfile = new UserProfile();
			User user = new User();
			
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			String email = request.getParameter("email");
			String tel = request.getParameter("tel");
			String address = request.getParameter("address");
			
			
			userProfile.setEmail(email);
			userProfile.setTel(tel);
			userProfile.setAddress(address);
			user.setUserName(username);
			
			BalzMD5Generate generate = new BalzMD5Generate();
			password = generate.generatePasswordMD5(password).trim();
			logger.debug("MD5 Password > "+password);
			
			user.setPassword(password);
			user.setUserProfile(userProfile);
			
			registerService.register(user);
			
		}
		
	}
	
	private boolean usernameCheck(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter out = response.getWriter();
		boolean usernameExist = true;
		String username = request.getParameter("username");
		
		RegisterService registerService = new RegisterServiceImpl();
		usernameExist = registerService.checkUsernameExist(username.trim());
		
		logger.debug("usernameExist >> "+usernameExist);
		
		if(usernameExist){
			out.println(1);
		}else{
			out.println(0);
		}
		
		return usernameExist; 
	}
	
	private boolean emailCheck(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter out = response.getWriter();
		boolean emailExist = true;
		String email = request.getParameter("email");
		
		RegisterService registerService = new RegisterServiceImpl();
		emailExist = registerService.checkEmailExist(email.trim());
		
		logger.debug("emailExist >> "+emailExist);
		
		if(emailExist){
			out.println(1);
		}else{
			out.println(0);
		}
		
		return emailExist; 
	}
	
	
}
